<template>
	<view>
		<u-number-box :max="max" :min="min" :step="step" :disabled="disabled || !writable" v-model="val"></u-number-box>
	</view>
</template>

<script>
export default {
	data() {
		return {
			val: 1,
			validator: false
		};
	},
	props: {
		value: {
			type: Number,
			default: 0
		},
		disabled: {
			type: Boolean,
			default: false
		},
		writable: {
			type: Boolean,
			default: true
		},
		max: {
			type: Number,
			default: 10
		},
		min: {
			type: Number,
			default: 10
		},
		step: {
			type: Number,
			default: 1
		},
		name: {
			type: String,
			default: ''
		},
		isDigit: { // 是否限制小数点后几位数
			type: Boolean,
			default: true
		},
		digit: { // 保留几位数，isDigit为true生效	
			type: Number,
			default: 2
		}
	},
	watch: {
		value: {
			handler () {
				this.val = this.value || 0
			},
			immediate: true,
			deep: true
		},
		val: {
			handler () {
				if(this.isDigit) {
					if(String(this.val).indexOf('.') >= 0) {
						if(String(this.val).slice(String(this.val).indexOf('.') + 1).length > this.digit) {
							uni.showToast({
								title: this.name + `最长支持${this.digit}位小数`,
								icon: 'none'
							})
							setTimeout(() => {
								this.val = Number(this.val.toFixed(this.digit))
								this.$emit('input', this.val)
							})
						}
					}
				}
				this.$emit('input', this.val)
			},
			immediate: true
		}
	}
}
</script>
<style scoped>
/deep/ .u-number-box__input {
	width: 45%!important;
}
</style>